package com.efb.module.ui.loading

import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.ViewTreeObserver.OnGlobalLayoutListener
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import com.inspector.app.R

/**
 * 按钮交互弹窗
 */
class ProgressDialogFragment : DialogFragment() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NO_TITLE, android.R.style.Theme_NoTitleBar_Fullscreen)
    }

    private var inflate: View? = null

    override fun onCreateView(inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?): View? {
        dialog?.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        inflate = inflater.inflate(R.layout.anim_progressdialog, container, false)
        if (!tempTips.isNullOrEmpty()) {
            inflate?.findViewById<TextView>(R.id.itemTVTips)?.visibility = View.VISIBLE
            inflate?.findViewById<TextView>(R.id.itemTVTips)?.text = tempTips
        } else {
            inflate?.findViewById<TextView>(R.id.itemTVTips)?.visibility = View.GONE
        }
        return inflate
    }

    fun showType(type: Int, tips: String? = null) {
        try {
            if (!tips.isNullOrEmpty()) {
                inflate?.findViewById<TextView>(R.id.itemTVTips)?.visibility = View.VISIBLE
                inflate?.findViewById<TextView>(R.id.itemTVTips)?.text = tips
            }
            when (type) {
                ProgressDialog_Succeed -> { //成功
                    inflate?.findViewById<View>(R.id.boxProgress)?.visibility = View.GONE
                    inflate?.findViewById<View>(R.id.boxTip)?.visibility = View.VISIBLE
                    inflate?.findViewById<View>(R.id.boxTip)
                        ?.setBackgroundResource(R.mipmap.img_finish)
                }
                ProgressDialog_Failed -> { //失败
                    inflate?.findViewById<View>(R.id.boxProgress)?.visibility = View.GONE
                    inflate?.findViewById<View>(R.id.boxTip)?.visibility = View.VISIBLE
                    inflate?.findViewById<View>(R.id.boxTip)
                        ?.setBackgroundResource(R.mipmap.img_error)
                }
                else -> {
                    inflate?.findViewById<View>(R.id.boxProgress)?.visibility = View.VISIBLE
                    inflate?.findViewById<View>(R.id.boxTip)?.visibility = View.GONE
                }
            }

            inflate?.findViewById<RelativeLayout>(R.id.itemRootView)?.viewTreeObserver?.addOnGlobalLayoutListener(
                OnGlobalLayoutListener {
                    val itemRootView = inflate?.findViewById<RelativeLayout>(R.id.itemRootView)
                    if (itemRootView != null) {
                        inflate?.findViewById<RelativeLayout>(R.id.spaceRootView)?.layoutParams =
                            RelativeLayout.LayoutParams(itemRootView.width, itemRootView.height)
                    }
                })
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    fun singletonShow() {
        if(manager==null) return
        show(manager!!, "ProgressDialogFragment")
    }

    /*override fun show(manager: FragmentManager, tag: String?) {
//        super.show(manager, tag)
        try {
            val ft = manager.beginTransaction()
            ft.add(this, tag)
            ft.commitAllowingStateLoss()
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }*/

    companion object {
        private var manager: FragmentManager? = null
        private var tempTips: String? = null
        const val ProgressDialog_Loading = 0
        const val ProgressDialog_Succeed = 1
        const val ProgressDialog_Failed = 2

        @JvmStatic
        fun create(manager: FragmentManager? = null,
            tempTips: String? = null): ProgressDialogFragment {
            this.manager = manager
            this.tempTips = tempTips
            return ProgressDialogFragment()
        }
    }
}